﻿@model LanguageModel
@inject AdminAreaSettings adminAreaSettings
<vc:admin-widget widget-zone="language_details_resources_top" additional-data="Model" />
<div class="panel panel-default">
    @if (String.IsNullOrEmpty(Model.Id))
    {
        <div class="note note-info">
            @T("Admin.Configuration.Languages.Resources.SaveBeforeEdit")
        </div>
    }
    <div class="form-horizontal">
        <div class="form-body">
            <div class="form-group">
                <admin-label asp-for="Search.ResourceName" />
                <div class="col-md-9 col-sm-9">
                    <admin-input asp-for="Search.ResourceName" />
                </div>
            </div>
            <div class="form-group">
                <admin-label asp-for="Search.ResourceValue" />
                <div class="col-md-9 col-sm-9">
                    <admin-input asp-for="Search.ResourceValue" />
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-9 col-md-offset-3">
                    <button type="button" id="search-resources" class="btn btn-primary btn-search">
                        <i class="fa fa-search"></i>
                        @T("Admin.Common.Search")
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div id="resources-grid"></div>
    </div>
</div>
<vc:admin-widget widget-zone="language_details_resources_bottom" additional-data="Model" />
@if (!String.IsNullOrEmpty(Model.Id))
{
    <script>
            $(document).ready(function() {
                $("#resources-grid").kendoGrid({
                    dataSource: {
                        transport: {
                            read: {
                                url: "@Html.Raw(Url.Action("Resources", "Language", new {languageId = Model.Id }))",
                                type: "POST",
                                dataType: "json",
                                //contentType: "application/json",
                                data: additionalData
                            },
                            create: {
                                url: "@Html.Raw(Url.Action("ResourceAdd", "Language", new {languageId = Model.Id }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            update: {
                                url: "@Html.Raw(Url.Action("ResourceUpdate", "Language"))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            destroy: {
                                url: "@Html.Raw(Url.Action("ResourceDelete", "Language"))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                        },
                        schema: {
                            data: "Data",
                            total: "Total",
                            errors: "Errors",
                            model: {
                                id: "Id",
                                fields: {
                                    Name: { editable: true, type: "string" },
                                    Value: { editable: true, type: "string" },
                                    Id: { editable: false, type: "string" }
                                }
                            }
                        },
                        requestEnd: function(e) {
                            if (e.type == "create" || e.type == "update") {
                                this.read();
                            }
                        },
                        error: function(e) {
                            display_kendoui_grid_error(e);
                            // Cancel the changes
                            this.cancelChanges();
                        },
                        pageSize: @(adminAreaSettings.DefaultGridPageSize),
                        serverPaging: true,
                        serverFiltering: false,
                        serverSorting: false
                    },
                    pageable: {
                        refresh: true,
                        pageSizes: [@(adminAreaSettings.GridPageSizes)]
                    },
                    toolbar: [{ name: "create", text: "@T("Admin.Common.AddNewRecord")" }],
                    editable: {
                        confirmation: false,
                        mode: "inline"
                    },
                    scrollable: false,
                    columns: [
                        {
                            field: "Name",
                            title: "@T("Admin.Configuration.Languages.Resources.Fields.Name")",
                            width: 300,
                            filterable: true,
                            sortable: true
                        }, {
                            field: "Value",
                            title: "@T("Admin.Configuration.Languages.Resources.Fields.Value")",
                            width: 300,
                            filterable: true,
                            sortable: true

                        }, {
                            command: [
                                {
                                    name: "edit",
                                    text: {
                                        edit: "@T("Admin.Common.Edit")",
                                        update: "@T("Admin.Common.Update")",
                                        cancel: "@T("Admin.Common.Cancel")"
                                    }
                                }, {
                                    name: "destroy",
                                    text: "@T("Admin.Common.Delete")"
                                }
                            ],
                            width: 200,
                            filterable: false
                        }
                    ]
                });
            });

    </script>
    <script type="text/javascript">
            $(document).ready(function() {
                $('#search-resources').click(function() {
                    var grid = $('#resources-grid').data('kendoGrid');
                    grid.dataSource.page(1);
                    return false;
                });
            });

            function additionalData() {
                var data = {
                    ResourceName: $('#@Html.FieldIdFor(model => model.Search.ResourceName)').val(),
                    ResourceValue: $('#@Html.FieldIdFor(model => model.Search.ResourceValue)').val()
                };
                addAntiForgeryToken(data);
                return data;
            }

            $("".concat("#@Html.FieldIdFor(model => model.Search.ResourceName),",
                "#@Html.FieldIdFor(model => model.Search.ResourceValue)")).keydown(function(event) {
                if (event.keyCode === 13) {
                    $("#search-resources").click();
                    return false;
                }
            });
    </script>

}
